<?php
namespace App\Http\Controllers\ApiApp\Stock;

use App\DBs\Stock\SourceDB;
use Illuminate\Http\Request;

class SourceController extends BaseController
{
    /**
     * 素材
     */

    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new SourceDB();
    }

    /**
     * 列表
     */
    public function getIndex(Request $request)
    {
        //接口权限值
        $access = $request->post('appAccess','');
        if (env('APP_ENV')!='dev' &&
            $access!=$this->db->getAccessLast()) {
            return $this->toAppJson(-1);
        }
        $uid = $request->post('uid',0);
        $bid = $request->post('bid',0);
        if (!$uid || !$bid) {
            return $this->toAppJson(-2);
        }
        //获取创板信息
        $resBoard = $this->db->getBoardOneById($bid);
        if (!$resBoard) {
            return $this->toAppJson(-5, array(), '没有创板数据');
        }
        $vid = $resBoard['vid'];
        //获取库信息
        $resStock = $this->db->getStockOneById($vid);
        if (!$resStock) {
            return $this->toAppJson(-5, array(), '没有库数据');
        }
        //获取数据
        $resDb = $this->db->getAppIndex($uid, $bid);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-5,array(),$resDb['msg']);
        }
        $sourceList = $resDb['data'];
        $dataArr = array(
            'boardTitle' => $resStock['fileTitle'],
            'boardNum' => $resStock['plusNum'],
            'boardHtml' => $resStock['boardHtml'],
            'sourceList' => $sourceList,
        );
        return $this->toAppJson(0, $dataArr ,'获取成功');
    }

    /**
     * 更新
     */
    public function setUpdate(Request $request)
    {
        //接口权限值
        $access = $request->post('appAccess','');
        if (env('APP_ENV')!='dev' &&
            $access!=$this->db->getAccessLast()) {
            return $this->toAppJson(-1);
        }
        $uid = $request->post('uid',0);
        $sid = $request->post('sid',0);
//        $type = $request->post('type',0); //1素材地址，2文字，
        $link = $request->post('link',0); //素材地址或文字
        if (!$uid || !$sid || !$type || !$link) {
            return $this->toAppJson(-2);
        }
        //获取用户信息
        $resUser = $this->db->getUnameByUid($uid);
        //看看创板和素材有没有
        $resDb2 = $this->db->getAppOne($sid);
        if ($resDb2['code']!=0) {
            return $this->toAppJson(-5,array());
        }
        $oldOne = $resDb2['data'];
        $vid = $oldOne['vid']; $plus = $oldOne['plus'];
        $bid = $oldOne['bid'];
        //没有则重新添加
        $dataArr = array(
            'uid' => $uid,
            'uname' => $resUser,
            'vid' => $vid,
            'plus' => $plus,
            'bid' => $bid,
            'serial' => $serial,
            'type' => $type,
            'link' => $link,
            'created_at' => time(),
        );
        $resDb = $this->db->setUpdate($sid, $dataArr);
        if ($resDb['code']!=0) {
            return $this->toAppJson(-22,array(),$resDb['msg']);
        }
        //需要返回的信息
        $sourceId = $resDb['data']['lastId'];
        $resDb3 = $this->db->getAppOne($sourceId);
        if ($resDb3['code']!=0) {
            return $this->toAppJson(-5,array());
        }
        $dataArr2 = $resDb3['data'];
        return $this->toAppJson(0, $dataArr2);
    }
}